﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Net.Mime;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备维修管理
{
    /// <summary>
    /// 装备领取
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class EquipmentCollectionController : BaseController
    {
        private readonly IEquipmentCollectionService equipmentCollectionService;
        /// <summary>
        /// 装备领取
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="equipmentCollectionService"></param>
        public EquipmentCollectionController(IWebHostEnvironment environment, IEquipmentCollectionService equipmentCollectionService) : base(environment)
        {
            this.equipmentCollectionService = equipmentCollectionService;
        }
        /// <summary>
        /// 查询装备领取
        /// </summary>
        /// <param name="name">领取装备名称</param>
        /// <param name="collectionUser">领取人</param>
        /// <param name="collectionDate">领取时间</param>
        /// <param name="equipmentType">领取装备种类</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllEquipmentCollection(string? name, string? collectionUser, DateTime? collectionDate, string? equipmentType,int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = equipmentCollectionService.GetEquipmentCollection(name, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据装备id查询装备领取
        /// </summary>
        /// <param name="id">装备id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentCollectionById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = equipmentCollectionService.GetEquipmentCollectionById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加装备领取
        /// </summary>
        /// <param name="equipmentCollection">装备领取实体类</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddEquipmentCollection(EquipmentCollection equipmentCollection)
        {
            equipmentCollection.CreateUserId = base.BaseGetNowUserId();
            equipmentCollection.CreateUserName = base.BaseGetNowUserName();

            var res = equipmentCollectionService.AddEquipmentCollection(equipmentCollection);
            if (res)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败", "500"));
        }
        /// <summary>
        /// 修改装备领取管理
        /// </summary>
        /// <param name="EquipmentCollection">装备领取实体类</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdEquipmentCollection(EquipmentCollection EquipmentCollection)
        {
            if (EquipmentCollection.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = equipmentCollectionService.UpdEquipmentCollection(EquipmentCollection);
            if (res)
            {          
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }
        /// <summary>
        /// 删除装备领取管理
        /// </summary>
        /// <param name="id">装备领取id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelEquipmentCollection(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = equipmentCollectionService.DelEquipmentCollection(id);
            if (res)
            {         
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 装备领取记录导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EquipmentReceiveDown()
        {
            var list = equipmentCollectionService.GetEquipmentReceiveMsgDown();
            if (list != null)
                return EquipmentReceiveDown(list);
            else
                return Ok();
        }

    }
}
